package com.wkbb.sports.utils;

import java.util.List;
import java.util.Map;

import com.wkbb.sports.dto.StatisticsAvgScoreDto;
import com.wkbb.sports.model.HlifeTrain;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StatisticsAvgScore {
	
	
	private List<HlifeTrain> list;
	
	private List<Long> listOrgids;
	
	private StatisticsAvgScoreDto  statisticsAvgScore = new StatisticsAvgScoreDto();
	
	
	 
	public StatisticsAvgScore(List<HlifeTrain> list,List<Long> listOrgids){
		
		this.list=list;
		this.listOrgids=listOrgids;
	}
	
	
	public StatisticsAvgScoreDto calAvgScore(Map map,int week){
		
		//班级男生平均成绩
		double maleSchoolAvg = 0.0;
		//班级女生平均成绩
		double femaleSchoolAvg = 0.0;
		//本市男生平均成绩
		double maleCityAvg=0.0;
		//本市女生平均成绩
		double femaleCityAvg=0.0;
		//单个学校女生平均成绩
		double singlFemaleSchoolAvg = 0.0;
		//单个学校男生平均成绩
		double singleMaleSchoolAvg = 0.0;
		
		
		
		for(Long orgID:listOrgids){
			//单次男生平均成绩
			double singleMaleAvg = 0.0;
			//单次女生平均成绩
			double singleFemalAvg=0.0;
			
			//单次班级男生平均成绩
			double singleClassMaleAvg=0.0;
			
			//单次班级女生平均成绩
			double singleClassFemaleAvg=0.0;
			
			//学校训练次数
			int i = 0;
			
			int j=0;
			
			for(HlifeTrain hlifeTrain:list){
				if(compareValue(orgID,hlifeTrain.getOrgId())){
					singleFemalAvg += hlifeTrain.getFemaleTotal()==0.0?0.0:hlifeTrain.getFemaleScore()/hlifeTrain.getFemaleTotal();
					singleMaleAvg += hlifeTrain.getMaleTotal()==0.0?0.0:hlifeTrain.getMaleScore()/hlifeTrain.getMaleTotal();
					i++;
					
					if(compareValue((Number) map.get("classId"),hlifeTrain.getClassId())){
						singleClassMaleAvg+=hlifeTrain.getMaleTotal()==0.0?0.0:hlifeTrain.getMaleScore()/hlifeTrain.getMaleTotal();
						singleClassFemaleAvg+=hlifeTrain.getFemaleTotal()==0.0?0.0:hlifeTrain.getFemaleScore()/hlifeTrain.getFemaleTotal();
						j++;
					}
				}
				
			}
			
			
			singlFemaleSchoolAvg+=(i==0?0.0:singleFemalAvg/i);
			singleMaleSchoolAvg+=(i==0?0.0:singleMaleAvg/i);
			
			
			//本校班级男女平均分
			maleSchoolAvg = (j==0?0.0:singleClassMaleAvg/j);
			femaleSchoolAvg =(j==0?0.0:singleClassFemaleAvg/j);
			
//			if(compareValue(orgID,(Number) map.get("orgId"))){
//				maleSchoolAvg = (i==0?0.0:singleFemalAvg/i);
//				femaleSchoolAvg =(i==0?0.0:singleMaleAvg/i);
//			}
			
			
		}
		
		//本市男女平均成绩
		maleCityAvg=singleMaleSchoolAvg/listOrgids.size();
		femaleCityAvg=singlFemaleSchoolAvg/listOrgids.size();
		
		statisticsAvgScore.setWeek(String.valueOf(week));
		
		statisticsAvgScore.setFemaleCityScore(femaleCityAvg);
		statisticsAvgScore.setFemaleSchoolScore(femaleSchoolAvg);
		statisticsAvgScore.setMaleCityScore(maleCityAvg);
		statisticsAvgScore.setMaleSchoolScore(maleSchoolAvg);
		
		
		return statisticsAvgScore;
		
	}
	
	
	private boolean compareValue(Number n1, Number n2) {
    	if(n1 == null || n2 == null) {
    		return false;
    	}
    	if(n1 instanceof Long) {
    		return n1.longValue() == n2.longValue();
    	}
    	if(n1 instanceof Byte) {
    		return n1.byteValue() == n2.byteValue();
    	}
    	return n1.intValue() == n2.intValue();
    }

   
}
